import express from "express";
import { addSql, getResult, listSql, putSql } from "../../../util/operateSql.js";

const router = express.Router()

const DictsTable = 'activity_master.dicts'
const DictItemsTable = 'activity_master.dict_items'

router.get('/list/:type',(req,res,next) => {
  const { type } = req.params
  const sql = listSql(DictItemsTable,{parent_field: type})
  getResult(sql).then((result) => {
    res.json(req.app.locals.success(result))
  }).catch((err) => {
    res.json(req.app.locals.error(err))
  });
})


router.post('/add/:type',(req,res,next) => {
  const { type } = req.params
  const {field,name,value,sort,color} = req.body
  const sql = addSql(DictItemsTable,{field,name,value,sort,color,parent_field:type})
  getResult(sql).then((result) => {
    console.log('result: ', result);
    res.json(req.app.locals.success('提交成功'))
  }).catch((err) => {
    console.log('err: ', err);
    res.json(req.app.locals.error(err))
  });
})
router.delete('/list')
router.put('/update/:type',(req,res,next) => {
  const {type} = req.params
  const {id, name} = req.body
  const sql = putSql(DictItemsTable,{name},{id,parent_field:type},'=','and')
  getResult(sql).then((result) => {
    res.json(req.app.locals.success('修改成功'))
  }).catch((err) => {
    res.json(req.app.locals.error(err))
  });
})

export default router